<?
//Import Classes
	require_once('classes/session.php');
	require_once('classes/sql_wtc.php');
	require_once('classes/functions.php');
	
	$myDB = new MySQL_class();
	$myDB->Create();

//Get Parameters
	if (isset($_POST['act'])) $act=$_POST['act'];
	else if (isset($_GET['act'])) $act=$_GET['act'];
		else $act='view';

//Do......		
	$myalert='';

//Add item	
	if ($act=='add' && isset($_GET['pid'])&&$_SESSION['SID']==$_GET['SID']) {
		$pid=$_GET['pid'];
		if (isset($_SESSION['cart'])) 
			$cart  = $_SESSION['cart'];
		else $cart = array();

		$find=0;

		
		for ($i=0;$i<count($cart) && $find<1;$i++)	{
			if ($cart[$i][0]==$pid) {
				$cart[$i][1]++;
				$alert=checkStock($cart[$i][0],$cart[$i][1]);
				$myalert.=$alert;
				$find	= 1;
			}
		}
		if ($find==0) {
			$cart[$i][0]	= $pid;
			$cart[$i][1]	= 1;
		}
		$_SESSION['cart']=$cart;
	}
//Delete item	
	if ($act=='delete' && isset($_GET['pid'])) {
		$pid=$_GET['pid'];
		if (isset($_SESSION['cart'])) 
			$cart  = $_SESSION['cart'];
		else $cart = array();

		$find=0;
		for ($i=0;$i<count($cart) && $find<1;$i++)	{
			if ($cart[$i][0]==$pid) {
				$cart[$i][0]=-1;
				$cart[$i][1]=0;
				$find	= 1;
			}
		}
		$_SESSION['cart']=$cart;
	}
//Modify item
	if ($act=='recalculate') {
		$qty=$_POST['qty'];
		if (isset($_SESSION['cart'])) 
			$cart  = $_SESSION['cart'];
		else $cart = array();

		for ($i=0;$i<count($cart);$i++) {
			$cart[$i][1]=$qty[$cart[$i][0]];
			$alert=checkStock($cart[$i][0],$cart[$i][1]);
			$myalert.=$alert;
		}
		$_SESSION['cart']=$cart;
	}

//To purchase
	if ($act=='next') {
		$qty=$_POST['qty'];
		if (isset($_SESSION['cart'])) 
			$cart  = $_SESSION['cart'];
		else $cart = array();

		for ($i=0;$i<count($cart);$i++) {
			$cart[$i][1]=$qty[$cart[$i][0]];
			$alert=checkStock($cart[$i][0],$cart[$i][1]);
			$myalert.=$alert;
		}
		$_SESSION['cart']=$cart;
		if (!$myalert) {
			@mysql_close();
			header("Location: sign_in.php");
			exit();
		}	
	}

	if ($myalert) $alert='<script>alert("'.$myalert.'");</script>';
	
//Functions	

	$menuhtml		= buildMenu($cat_lev,$category,$cat_par,'product.php');
	$viewcart		= viewCart($_SESSION['cart']);
	if (!isset($_SESSION['view'])||!$_SESSION['view'][0][0]) {
		@mysql_close();
		header ("Location: empty_cart.php");
		exit();
	}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>Jonesky eShopping</title>


<link href="css/main.css" rel="stylesheet" type="text/css">

<script language="javascript">
<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function confirm_delete(id) {
	if (confirm("Are you sure to delete this item ?")) {
		MM_goToURL('this','<?=$PHP_SELF?>?act=delete&pid='+id);
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->
</script>

</head>

<body>
<table width="770" align="center" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="85" align="center" valign="top">
	  <table width="100%" height="84"  border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="205" rowspan="3" align="center" valign="middle" background="images/index_LOGO_t1-1.gif">&nbsp;</td>
          <td width="1" rowspan="3"></td>
          <td height="57" align="right" background="images/index_CH_t1-2-1.gif"><table width="430" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="425" height="34" align="right" background="images/index_CH_t1-2-1-2.gif"><a href="../Jonesky_CH/view_cart.php"><img src="images/jonesky_chinese.gif" border="0"></a></td>
			  <td width="5"></td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td height="1"></td>
        </tr>
        <tr>
          <td height="26" align="right" background="images/index_SVC_t1-2-2.gif"><table width="" height="18" border="0" cellpadding="0" cellspacing="0">
            <tr align="left" valign="middle">
              <td width="115"><a class=s3 href="shipping_charge_list.php">- Shipping Charge </a></td>
              <td width="75"><a class=s3 href="view_cart.php">- View Cart </a></td>
              <td width="75"><a class=s3 href="check_out_delivery_details.php">- Check Out</a> </td>
              <td width="155"><a class=s3 href="claim.php">- Redemption Centre Info </a></td>
            </tr>
          </table></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td height="111" bgcolor="#FF0000"><img src="images/<?=randomBanner()?>" width="770" height="111"></td>
  </tr>
    <tr>
    <td height="1"></td>
  </tr>
  <tr>
    <td height="475" valign="top"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="mytd">
      <tr>
        <td height="31"><table width="100%" height="31"  border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="710" background="images/red_navigate.gif"><table width="100%" height="31"  border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="43" align="center"><a class=s3 href="index_eng.php">Home</a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="59" align="center"><a class=s3 href="about_us.php">About us </a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="86" align="center"><a class=s3 href="new_products.php">New Products </a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="68" align="center"><a class=s3 href="promotion.php">Promotion</a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="33" align="center"><a class=s3 href="faq.php">FAQ</a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="130" align="center"><a class=s3 href="terms_and_condition.php">Terms and Conditions</a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="83" align="center"><a class=s3 href="return_policy.php">Return Policy </a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="93" align="center"><a class=s3 href="delivery_policy.php">Delivery Policy</a> </td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="71" align="center"><a class=s3 href="contact_us.php">Contact us </a></td>
                <td width="1"><img src="images/whiteLine.gif"></td>
                <td width="35" align="center"><a class=s3 href="news.php">News</a></td>
              </tr>
            </table></td>
            <td><img src="images/red_navigate_right.gif" width="36" height="31"></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td height="381"><table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="4"></td>
            <td width="192" valign="top">
			<table border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="2" bgcolor="#FFFFFF"></td>
                <td width="184" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td height="2" bgcolor="#FFFFFF"></td>
                  </tr>
                  <tr>
                    <td height="28"><img src="images/products0.gif" width="184" height="28"></td>
                  </tr>
                  <tr>
                    <td height="2" bgcolor="#FFFFFF"></td>
                  </tr>
                  <tr>
                    <td height="80" background="images/products3.jpg">
                      <form name="searchbox" action="product_search_result.php" method="post"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td height="20">&nbsp;&nbsp;&nbsp;&nbsp;<a class=s2>Keywords</a></td>
                        </tr>
                        <tr>
                          <td>&nbsp;&nbsp;&nbsp;<input name="search" type="text" class="text" id="search" size="20"></td>
                        </tr>
                        <tr>
                          <td height="28">&nbsp;&nbsp;&nbsp;<input name="submit" type="submit" class="button" id="submit" onClick="MM_validateForm('search','','R');return document.MM_returnValue" value="Search"></td>
                        </tr>
                      </table></form>
</td>
                  </tr>
					<?=$menuhtml?>
                </table></td>
                <td width="6" valign="top" background="images/index_SVC_t1-2-2.jpg">&nbsp;</td>
              </tr>
              <tr>
                <td width="2" bgcolor="#FFFFFF"></td>
                <td height="12" background="images/Pro_Search_bottom.gif">&nbsp;</td>
                <td width="6"><img src="images/Pro_Search_circle.gif" width="6" height="12"></td>
              </tr>
            </table>
			</td>
            <td width="574" valign="top">
			
			
			
			
			<form action="" method="post" name="form1">
			<table width="560" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td height="65" valign="bottom"><img src="images/procedure1.gif" width="562" height="50"></td>
              </tr>
              <tr>
                <td height="10"></td>
              </tr>
              <tr>
                <td width="560" height="23"><table width="560" height="23"  border="0" cellpadding="0" cellspacing="0" background="images/table_line.jpg">
              <tr>
                            <td width="82" class="s4">&nbsp; Item</td>
                <td width="202" class="s4">Description</td>
                <td width="61" class="s4"><div align="center">Qty</div></td>
                <td width="76" align="center" class="s4">Unit Price </td>
                <td width="87" align="center" class="s4">Amount</td>
                <td width="52" class="s4"><div align="center">Delete</div></td>
              </tr>
            </table></td>
              </tr>
              <tr>
                <td>
				
				<?=$viewcart[0]?>
				
				
				</td>
              </tr>
              <tr>
                      <td class="s9"><br>
                        <br>
                  * Total does not include any shipping charge and redemption fee.<br>
                        &nbsp;&nbsp;&nbsp;Final&nbsp; amount will be shown on 
                        Order Summary page.  <br>
                        <br>
                      </td>
              </tr>
              <tr>
                <td align="center"><p><br>
                          <a href="product.php"><img src="images/cont_to_purchase.gif" width="161" height="19" border="0"></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#"><img src="images/ptop_up.gif" width="161" height="19" border="0" onClick="document.form1.act.value='next';document.form1.submit();"></a>&nbsp;&nbsp;&nbsp;&nbsp;
                          <input name="submit" type="image" id="submit" src="images/recalculate.gif" width="161" height="19" <?=$viewcart[1]?>></p>
                  <p><a href="shipping_charge_list.php"><img src="images/ship_charge_table.gif" width="168" height="19" border="0"></a>
                    <input name="act" type="hidden" id="act" value="recalculate">
                  </p></td>
              </tr>
            </table>
			</form>
      </td>
          </tr>
        </table></td>
      </tr>
      <tr>
          <td height="80" align="center" valign="bottom" bgcolor="#FFFFFF"><span class="s1">Copyright 
            &copy; 2004, All Rights Reserved by Jonesky Limited. <br>
            &nbsp;&nbsp; </span></td>
      </tr>
    </table></td>
  </tr>

</table>
<?=$alert?>
</body>
</html>
<?
@mysql_close();
?>